package com.zhxy.manager.controller;


import com.alibaba.excel.EasyExcel;
import com.zhxy.manager.entity.College;
import com.zhxy.manager.entity.Student;
import com.zhxy.manager.service.intf.StudentService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/student/")
public class StudentController {
   @Autowired
    private StudentService service;

    @PostMapping("save.do")
    public R save(Student student){

        return service.save(student);
    }
    @GetMapping("page.do")
    public PageBean page(int page, int limit){

        return service.queryPage(page,limit);
    }
    //修改
    @PostMapping("update.do")
    public R update(Student student){

        return service.update(student);
    }
    //删除
    @PostMapping("del.do")
    public R del(int id){
        return service.del(id);
    }
    //查询 条件  多表
    @GetMapping("pagewhere.do")
    public PageBean page(String name, int sex, int page, int limit){

        return service.queryPageGrade(name,sex,page,limit);
    }
    //批量删除 JSON格式的数据请求
    @PostMapping("delbatch.do")
    public R del(@RequestBody int[] ids){
        return service.delBatch(ids);
    }

    //数据导出
    @GetMapping("downall.do")
    public void down(HttpServletResponse response) throws IOException {
        //1.查询数据
        List<Student> list=service.queryAll();
        //2.设置响应消息头
        response.setHeader("Content-disposition", "attachment;filename=" +System.currentTimeMillis() + ".xlsx");
        //3.写出内容
        EasyExcel.write(response.getOutputStream(),College.class).
                sheet("自动导出").doWrite(list);
    }
}
